package com.nameli.admin.controller;

import com.nameli.SIM.bean.entity.sys.LoginBody;
import com.nameli.SIM.bean.entity.sys.SysPermission;
import com.nameli.SIM.bean.entity.sys.SysUser;
import com.nameli.SIM.bean.vo.LoginVo;
import com.nameli.SIM.util.RedisUtil;
import com.nameli.SIM.util.response.JsonResult;
import com.nameli.SIM.util.ReturnStatus;
import com.nameli.SIM.util.routers.RouterNode;
import com.nameli.admin.config.SecurityTools;
import com.nameli.admin.service.ShiroService;
import com.nameli.admin.service.SysMenuService;
import com.nameli.admin.service.SysUserService;
import com.nameli.admin.service.impl.system.SysPermissionService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author NAME-L
 * @Description 登录操作 控制层
 * @className HelloController
 * @date 2021-10-29 17:04:15
 */
@RestController
@CrossOrigin
public class LoginController {

    private static final Log logger = LogFactory.getLog(LoginController.class);

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private ShiroService shiroService;

    @Autowired
    private SysMenuService sysMenuService;

    @Autowired
    private SysPermissionService permissionService;

    @Autowired
    private RedisUtil redisUtil;

    @PostMapping("/login")
    public JsonResult login(HttpServletRequest request, @RequestBody LoginBody loginBody) {
        JsonResult jsonResult = null;
        logger.info("进入登录界面");
        String errorCode = "";//返回状态码
        String errorMsg = "";//返回的提示信息
        Map<String, Object> returnMap = new HashMap<>();
        String userName = loginBody.getUsername();
        String password = loginBody.getPassword();
        logger.info("userName:" + userName);
        logger.info("password:" + password);
        SysUser user = sysUserService.login(userName, password);
        if (user == null) {
            errorCode = ReturnStatus.NO_LOGIN;
            errorMsg = "登录失败";
            jsonResult = JsonResult.withoutData(errorCode, errorMsg);
        } else {
            String token = shiroService.createToken(user.getUserId());
            LoginVo loginVo = sysUserService.beanTransform(user);
            redisUtil.set(token, loginVo, 1200);
            returnMap.put("token", token);
            jsonResult = JsonResult.loginSuccessFul(returnMap);
        }
        return jsonResult;
    }

    @GetMapping("/403")
    public JsonResult unauthorizedRole() {
        logger.info("------没有权限-------");
        JsonResult jsonResult = null;
        String errorCode = "";//返回状态码
        String errorMsg = "";//返回的提示信息
        errorCode = ReturnStatus.FORBIDDEN;
        errorMsg = "没有权限";
        jsonResult = JsonResult.withoutData(errorCode, errorMsg);
        return jsonResult;
    }

    @GetMapping("/getUserInfo")
    public JsonResult getUserInfo(){
        logger.info("进入获取用户信息");
        JsonResult jsonResult = null;
        Map<String, Object> returnMap = new HashMap<>();
        SysUser user = SecurityTools.getLoginUser();
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        returnMap.put("user", user);
        returnMap.put("role", roles);
        returnMap.put("permissions", permissions);
        jsonResult = JsonResult.querySuccessFul(returnMap);
        return jsonResult;
    }

    /**
     * 根据登录用户生成菜单
     *
     * @return
     */
    @GetMapping("/getRouters")
    public JsonResult getRouters() {
        logger.info("进入路由菜单获取生成");
        Long userId = SecurityTools.getUserId();
        logger.info("当前登录用户id为:" + userId);
        JsonResult jsonResult = null;
        List<RouterNode<SysPermission>> returnList = sysMenuService.selectTreeByUserId(userId);
        jsonResult = JsonResult.querySuccessFul(returnList);
        return jsonResult;
    }
}